/**
 * Object for each of the trigger entries found in a blip
 */
package hyperlinkyclassic;

public class TriggerEntry implements Comparable<TriggerEntry>{

        private int startPos;		// Holds the starting position of the entry
        private String delimiter;	// Holds the ending delimiter of the trigger
        private String text;		// Holds the text of the trigger including the markup
        private String searchText;	// just the search string;
        private Trigger trigger;	// Which trigger
        
        public TriggerEntry(int startPos, String delimiter, String text, Trigger trigger) {
                this.startPos = startPos;
                this.delimiter = delimiter;
                this.text = text;
                this.trigger = trigger;
                processEntry();
        }
        
        public void processEntry() {
        	int i = 0, j = 0;
        	searchText = text;
        	// find the beginning of the text, eventually take into account length of trigger!
        	i = searchText.indexOf('_') + 1;
        	if(delimiter.length() > 0) { // there's a delimiter!
        		i += delimiter.length();
        		j = searchText.lastIndexOf(delimiter);
        	}
        	else
        		j = searchText.length();
        	searchText = searchText.substring(i, j);
        	// searchText = searchText + " (" + trigger.getSearchName() + ": " + trigger.getSearchLink() + searchText + ")"; // NEEDS TO BE HTML ESCAPED!!
        }
        
        /* Getters*/
        
        public String getDelimiter() {
        	return delimiter;
        }
        
        public String getHTMLEncodedSearchText() {
        	// clean up the search string... leave spaces??
        	return searchText.replaceAll("[^\\w\\s\\.@]", "");
        }
        
        public int getStartPos() {
                return startPos;
        }

        public String getText() {
        	return text;
        }
        
        public String getSearchText() {
        	return searchText;
        }
        
        /**
         * @return the trigger
         */
        public Trigger getTrigger() {
                return trigger;
        }

        @Override
        public int compareTo(TriggerEntry te) {
                return this.startPos - te.startPos;
        }

}
